<template>
	<view class="main_page">
		<!-- 导航栏 -->
		<view class="head_box active">
			<cu-custom-new :isBack="true" :backColor="$headerBgColor" @navigateBack="navigateBack">
				<block slot="content">
					<text class="nav-title shopro-selector-rect">任务中心</text>
				</block>
			</cu-custom-new>
		</view>
		<view class="content_box" style="margin-top: -4rpx;overflow: hidden;">
			<scroll-view class="scroll-box" scroll-y scroll-with-animation enable-back-to-top>
				<view class="wrapper" v-for="(item, i) in list" :key="i">
				    <view class="commonbox">
				        <view class="topbar">
				            <span class="day text">{{item.time}}</span>
				            <!-- <span class="tiem text">上午6:00</span> -->
				        </view>
				        <view class="content">
				            <view class="contimg video_layer_box" >
				                <image :src="item.image" v-if="item.type == 0" class="contimg_img" mode=""></image>
												<video :src="item.video" v-if="item.type == 1" :ref="`video${item.id}`" class="contimg_img" @play="play(item)" @pause="pause(item)" controls></video>
												<view class="video_layer"></view>
										</view>
				            <navigator :url="`./taskDetails?id=${item.id}`" class="contit">
				                <p class="title">{{item.title}}</p>
				            </navigator>
				        </view>
				    </view>
				</view>
				<!-- 空白页 -->
				<shopro-empty v-if="!list.length && !isLoading" :emptyData="emptyData"></shopro-empty>
				<!-- 加载更多 -->
				<u-loadmore v-if="list.length" height="80rpx" :status="loadStatus" icon-type="flower" color="#ccc" />
				
				<!-- load -->
				<shopro-load v-model="isLoading"></shopro-load>
			</scroll-view>
		</view>
		<!-- 登录提示 -->
		<shopro-login-modal></shopro-login-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				emptyData: {
					img: this.$IMG_URL + '/imgs/empty/empty_goods.png',
					tip: '暂无数据~'
				},
				list: [],
				listParams: {
					page: 1
				},
				isLoading: true, //loading和空白页。
				loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
				lastPage: 1,
				seconds: 0,
				time: 0,
				timeSet: null
			};
		},
		// 触底加载更多
		onReachBottom() {
			if (this.listParams.page < this.lastPage) {
				this.listParams.page += 1;
				this.getList();
			}
		},
		onLoad() {
			this.getList()
		},
		methods:{
			navigateBack() {
				let that = this
				this.seconds = 0
				this.time = 0
				if(this.timeSet) {
					clearInterval(that.timeSet)
				}
				uni.redirectTo({
					url: './index'
				})
			},
			getList() {
				let that = this;
				that.isLoading = true;
				that.loadStatus = 'loading';
				that.$apiBase('taskList', null).then(res => {
					that.isLoading = false;
					if (res.code === 1) {
						
						res.data.data.forEach((item, i) => {
							item.playFlag = true
						})
						that.list = [...that.list, ...res.data.data];
						that.lastPage = res.data.last_page;
						if (that.listParams.page < res.data.last_page) {
							that.loadStatus = 'loadmore';
						} else {
							that.loadStatus = 'nomore';
						}
					}
				});
			},
			play(item) {
				let that = this
				item.playFlag = true
				this.seconds = this.seconds == 0 ? item.seconds : this.seconds
				that.timeSet = setInterval(() => {
					if(that.seconds > 0) {
						that.seconds --
					} else{
						clearInterval(that.timeSet)
						that.readreward(item.id)
					}
				}, 1000)
			},
			pause(item) {
				let that = this
				item.playFlag = false
				clearInterval(that.timeSet)
			},
			readreward(id) {
				let that = this
				that.$apiBase('readreward', {
					id
				}).then(res => {
					if (res.code === 1) {
						uni.showToast({
							title: '获得奖励',							
							duration: 2000
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.wrapper{
    box-sizing: border-box;
    padding: 0 30rpx;
    margin-bottom: 50rpx;
		margin-top: 20rpx;
	.commonbox {
		// height:350rpx;
		.topbar {
			height: 50rpx;
			line-height: 50rpx;
			text-align: center;
			.text {
				color: #666666;
				font-size: 26rpx;
				&.day {
					margin-right: 10rpx;
				}
			}
		}
		.content {
			border-radius: 20rpx;
			box-shadow: 0px 4px 13px 3px rgba(0, 0, 0, 0.14);
			.contimg {
				// height: 206rpx;
				background: pink;
				border-radius: 20rpx 20rpx 0 0;
				.contimg_img {
					display: block;
					// height: 206rpx;
					height: 320rpx;
					width: 100%;
					border-radius: 20rpx 20rpx 0 0;
				}
			}
			.contit {
				height: 94rpx;
				line-height: 94rpx;
				background: #fff;
				box-sizing: border-box;
				padding:0 30rpx;
				border-radius: 0 0 20rpx 20rpx;
				.title {
					color: #000;
					font-size: 28rpx;
					font-family: PingFang SC;
					text-overflow:ellipsis;
					overflow: hidden;
					white-space: nowrap;
				}                 
			}	                
		}                
	}
}
</style>
